﻿using System;
using HelpdeskServices.Properties;

public partial class ConvertToCase : System.Web.UI.Page
    {
        private string _emailId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                loadQueryString();
            }
            
            CrmCase caseBeingConverted = new CrmCase();
            Email emailBeingConverted = new Email();
            emailBeingConverted.EmailId = new Guid(_emailId);
            emailBeingConverted.Load();

            caseBeingConverted.SourceEmail = emailBeingConverted;
            caseBeingConverted.Create();
            caseBeingConverted.attachEmail();
            caseBeingConverted.DetachEmailFromQueue();


            Response.Redirect("../../cs/cases/edit.aspx?id={" + caseBeingConverted.IncedentId.ToString() + "}");
        }

        /// <summary>
        /// Converts the case.
        /// </summary>
        /// <param name="emailId">The email id.</param>
        /// <returns></returns>
        private string ConvertCase()
        {
            ConvertToCase convertEmailToCase = new ConvertToCase();
            convertEmailToCase._emailId = _emailId;
            return convertEmailToCase.ConvertCase();
        }

        /// <summary>
        /// Loads the query string.
        /// </summary>
        private void loadQueryString()
        {
            if (Request.QueryString.Count > 0)
            {
                _emailId = Request.QueryString[Resources.QueryStringParameterId].ToString();
            }
        }
    }
